/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author RENZO
 */
public class AbstractDAO {
    
    public static CallableStatement prepararConexionCall(String query){
        dataAccess.getConnection();
        CallableStatement cs = null;
        try {
            cs = dataAccess.getConnection().prepareCall(query);
        } catch (SQLException ex) {
            Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            return cs;
        }
    }
    
    public static PreparedStatement prepararConexionPrepared(String query){
        dataAccess.getConnection();
        PreparedStatement ps = null;
        try {
            ps = dataAccess.getConnection().prepareStatement(query);
        } catch (SQLException ex) {
            Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            return ps;
        }
    }
    
    public static PreparedStatement prepararConexionPreparedWithReturnKey(String query){
        dataAccess.getConnection();
        PreparedStatement ps = null;
        try {
            ps = dataAccess.getConnection().prepareStatement(query,PreparedStatement.RETURN_GENERATED_KEYS);
        } catch (SQLException ex) {
            Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            return ps;
        }
    }
    
    public static Boolean realizarConexionPOST(CallableStatement cs){
        try {
            if(cs.executeUpdate() == 1){
                return true;
            }else{
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    public static ResultSet realizarConexionGET(CallableStatement cs){
         ResultSet rs = null;
        try {
            rs = cs.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            return rs;
        }
    }
    
    public static Boolean realizarConexionPOST(PreparedStatement cs){
        try {
            if(cs.executeUpdate() == 1){
                return true;
            }else{
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    public static ResultSet realizarConexionGET(PreparedStatement cs){
         ResultSet rs = null;
        try {
            rs = cs.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            return rs;
        }
    }
}
